<?php

namespace Device\Service;
use Qiniu\Auth;

require_once LSFPATH . '/lib/php_qiniu/autoload.php';

class Device {
	private $_daoDeviceModel;

	private $ACCESS_KEY = 'iZ8ZTjEJrtM9xxC3CMinY2_6AzV_RV0UHnxzttcs';
	private $SECRET_KEY = '7NyRkha5qGZ20f-iBMQ4qed-lY56gthKVyYT4RNk';
	private $BUCKET = 'kllz-test';
	private $QN_HOST = 'http://kllz.kllz.top';

	public function __construct() {
		$this->_daoDeviceModel = \Lsf\Loader::Model('DaoDevice', TRUE);
	}

	/**
	 * 获取机器配置
	 */
	public function getDeviceConfig($deviceId){
		$where = [];
		$where['device_id'] = $deviceId;
		$device =  $this->_daoDeviceModel->select('*', $where, 'device_id ASC', 1, FALSE);
		if($device === FALSE){
			\Lsf\Loader::plugin('Log')->error(DATABASE_OPERATE_FAIL, [
				'model_name' => 'dao_device',
			]);
			return FALSE;
		}
		if(empty($device)){
			return [
				'group_id' => 0,
				'group_name' => ''
			];
		}
		$groupName = $this->_getGroupName($device[0]['group_id']);
		$result['id'] = $device[0]['id'];
		$result['device_id'] = $device[0]['device_id'];
		$result['group_id'] = $device[0]['group_id'];
		$result['group_name'] = $groupName;
		//$result['status'] = $device[0]['status'];
		//$result['is_show'] = $device[0]['is_show'];
		//$result['question_id'] = $device[0]['center_question_id'];
		return $result;
	}

	/**
	 * 设置机器配置
	 */
	public function setDeviceConfig($deviceId, $groupId){
		$where['device_id'] = $deviceId;
		$device =  $this->_daoDeviceModel->select('*', $where, 'device_id ASC', 1, FALSE);
		if($device === FALSE){
			\Lsf\Loader::plugin('Log')->error(DATABASE_OPERATE_FAIL, [
				'model_name' => 'dao_device',
			]);
			return FALSE;
		}
		// 没有则插入记录
		if(empty($device)){
			$device = [
				'device_id'   => $deviceId,
				'group_id'    => $groupId,
				'status'      => 1,
				'create_time' => date("Y-m-d H:i:s",time()),
				'extra_data'  => '',
				'update_time' => date("Y-m-d H:i:s",time()),
				'is_show'     => 0,
				'center_question_id'  => 0,
				'rush_group_id' => 0,
				'rush_gp_a' => 0,
				'rush_gp_b' => 0,
				'rush_question_id' => 0,
				're_rush_question_id' => 0,
				'rush_user_answer' => '',
				'rush_start_flag' => 2,
			];
			$id = $this->_daoDeviceModel->insert($device);
			if ($id === FALSE) {
				\Lsf\Loader::plugin('Log')->error(DATABASE_OPERATE_FAIL, [
					'model_name' => 'dao_device',
					'insert'     => $device
				]);
				return FALSE;
			}
			unset($device['create_time']);
			$device['group_name'] = $this->_getGroupName($device['group_id']);
			return $device;
		}else {
			$device = [
				'group_id'  => $groupId,
				'status'    => 1,
				'update_time' => date("Y-m-d H:i:s",time()),
				'is_show'     => 0,
				'center_question_id'  => 0,
				'rush_group_id' => 0,
				'rush_gp_a' => 0,
				'rush_gp_b' => 0,
				'rush_question_id' => 0,
				're_rush_question_id' => 0,
			];
			$where = [
				'device_id' => $deviceId,
			];
			$rows = $this->_daoDeviceModel->update($device, $where);
			if ($rows === 0) {
				\Lsf\Loader::plugin('Log')->error(DATABASE_OPERATE_FAIL, [
					'model_name' => 'dao_device',
					'update'     => $device,
					'where'      => $where,
				]);
			}
			if ($rows === FALSE) {
				\Lsf\Loader::plugin('Log')->error(DATABASE_OPERATE_FAIL, [
					'model_name' => 'dao_device',
					'update'     => $device,
					'where'      => $where,
				]);
				return FALSE;
			}
			$device['device_id'] = $deviceId;
			$device['group_name'] = $this->_getGroupName($device['group_id']);
			return $device;
		}
	}

	private function _getGroupName($groupId){
		switch ($groupId){
		case 3 :
			{
				$groupName = '主持人';
				break;
			}
		case 1 :
			{
				$groupName = 'A组';
				break;
			}
		case 2 :
			{
				$groupName = 'B组';
				break;
			}
		default :
			{
				$groupName = '';
			}
		}
		return $groupName;
	}

	/**
	 * 获取七牛token
	 */
	public function getQiNiuToken(){
		$auth = new Auth($this->ACCESS_KEY, $this->SECRET_KEY);
		$upToken = $auth->uploadToken($this->BUCKET);
		return [
			'token' => $upToken,
			'host' => $this->QN_HOST
		];
	}
}

